
clear all
set more off

use "$ismu\ISMU.dta" , clear

count  
replace income=income/1936.27*1000 if year==2001 // income in Liras 
merge m:1 year using "$ismu\icp_lombardia.dta"
drop if _m==1
drop _m
replace income=income/icp // real income at 2010 prices
mvencode income chldrn_tot, mv(0) o // some obs ==0 others ==. --> put all equal to 0
keep if missing(permit)==0&missing(education)==0&missing(age)==0&missing( religion )==0&missing(civil_status)==0&missing( citizenship )==0&missing( work_status )==0&missing( work_status_reg )==0&missing(w_reg)==0&missing(chldrn_tot)==0

replace age=19 if age==1986&year==2005
drop if age>100 // coding errors
keep if age>=20&age<=60

*  Define legal immigrants 
gen legal=(permit!=3)
replace legal=1 if (EU10&year>=2004)|(EU2&year>=2007)

* Individual characteristics 
rename EU10 eu2004
rename EU2 eu2007
gen group="eu2004" if eu2004
replace group="eu2007" if eu2007
replace group="others" if group=="" 
egen neweu=rmax(eu2004 eu2007)
gen othereasteurope=(int(citizen/100)==2&neweu==0)

gen college=education==4
gen highschool=education==3
gen high=(college|highschool)
gen dropout=(college==0&highschool==0)
gen male=gender==1
gen married=civil_stat==2
gen employed=(work_status_reg==3|work_status_reg==4)
gen legaljob=(work_status_reg==3)
gen illegaljob=(work_status_reg==4)
gen age2=age^2

gen agecat=1 if age>=20&age<=24 
replace agecat=2 if age>=25&age<=29
replace agecat=3 if age>=30&age<=34
replace agecat=4 if age>=35&age<=39
replace agecat=5 if age>=40&age<=44
replace agecat=6 if age>=45&age<=49

label define ageclass 1 "20-24" 2 "25-29" 3 "30-34" 4 "35-39" 5 "40-44" 6 "45-50"
label values agecat ageclass
gen lnincome=ln(income)
gen ihsincome=asinh(income)

preserve
save "$ismu\ismudescriptives.dta", replace
restore


********************************************************************************
* Table 1: Panel a. Labor market outcomes
* Number of observations in the dataset and summary statistics

tabstat employed legaljob illegaljob income male age married chldrn_tot highschool college, by(legal) stat(mean sem) c(s)
foreach var in employed legaljob illegaljob income male age married chldrn_tot highschool college {
	qui ttest $_var, by(legal)
	display `r(mu_2)'-`r(mu_1)'
	display `r(se)'
}

tabstat employed legaljob illegaljob income male age married chldrn_tot highschool college if male==1, by(legal) stat(mean sem) c(s)
foreach var in employed legaljob illegaljob income male age married chldrn_tot highschool college {
	qui ttest $_var if male==1, by(legal)
	display `r(mu_2)'-`r(mu_1)'
	display `r(se)'
}

tabstat employed legaljob illegaljob income male age married chldrn_tot highschool college if male==0, by(legal) stat(mean sem) c(s)
foreach var in employed legaljob illegaljob income male age married chldrn_tot highschool college {
	qui ttest $_var if male==0, by(legal)
	display `r(mu_2)'-`r(mu_1)'
	display `r(se)'
}


* Create Treatment (Policy change)

preserve
drop if eu2007==1
gen stacked=2004
keep if year>=2001&year<=2009
tab year, g(dy)
save stacked, replace
restore

preserve
drop if eu2004==1
gen stacked=2007
keep if year>=2004&year<=2012
tab year, g(dy)
append using stacked

forvalues i=1/2 {
	gen neweuxdy$_i=neweu*dy$_i
}

forvalues i=4/9 {
	gen neweuxdy$_i=neweu*dy$_i
}

gen neweuxd2y_1=neweu*(dy1==1|dy2==1)
gen neweuxd2y1=neweu*(dy4==1|dy5==1)
gen neweuxd2y2=neweu*(dy6==1|dy7==1)
gen neweuxd2y3=neweu*(dy8==1|dy9==1)
gen neweuxpost=neweu*(dy3==1|dy4==1|dy5==1|dy6==1|dy7==1|dy8==1|dy9==1)

save "$ismu\stacked.dta", replace
restore



********************************************************************************
* Table 1: Panel b. Other individual characteristics
* Number of observations in the dataset and summary statistics


foreach x of varlist male age married chldrn_tot college highschool dropout employed legaljob illegaljob income { 

qui reg `x' [aw=w_reg] if legal==1
di "`x' & " _b[_cons] 
di " & "_se[_cons]

qui reg `x' [aw=w_reg] if legal==0
di _b[_cons] 
di _se[_cons]

qui reg `x' legal [aw=w_reg] 
di _b[legal] 
di _se[legal]
}


preserve
keep if male==1
foreach x of varlist age married chldrn_tot college highschool dropout employed legaljob illegaljob income { 

qui reg `x' [aw=w_reg] if legal==1
di "`x' & " _b[_cons] 
di " & "_se[_cons]

qui reg `x' [aw=w_reg] if legal==0
di _b[_cons] 
di _se[_cons]

qui reg `x' legal [aw=w_reg] 
di _b[legal] 
di _se[legal]
}
restore


preserve
keep if male==0
foreach x of varlist male age married chldrn_tot college highschool dropout employed legaljob illegaljob income { 

qui reg `x' [aw=w_reg] if legal==1
di "`x' & " _b[_cons] 
di " & "_se[_cons]

qui reg `x' [aw=w_reg] if legal==0
di _b[_cons] 
di _se[_cons]

qui reg `x' legal [aw=w_reg] 
di _b[legal] 
di _se[legal]
}
restore

 
use "$ismu\stacked.dta", replace

xtile pct=income, nq(200)
drop if pct==200


********************************************************************************
*  Figure 4. 
*  The effect of the EU enlargements on the labor market outcomes of new EU citizens

*  Males
xi: areg legaljob neweuxd2y* stacked#year if gender==1, robust abs(citizen)
est store legaljob2male0
xi: areg legaljob neweuxd2y* stacked#year i.stacked*age i.stacked*age2 i.stacked*highschool i.stacked*college i.stacked*married if gender==1, robust abs(citizen)
est store legaljob2male

xi: areg illegaljob neweuxd2y* stacked#year if gender==1, robust abs(citizen)
est store illegaljob2male0
xi: areg illegaljob neweuxd2y* stacked#year i.stacked*age i.stacked*age2 i.stacked*highschool i.stacked*college i.stacked*married if gender==1, robust abs(citizen)
est store illegaljob2male

xi: areg empl neweuxd2y* stacked#year if gender==1, robust abs(citizen)
est store empl2male0
xi: areg empl neweuxd2y* stacked#year i.stacked*age i.stacked*age2 i.stacked*highschool i.stacked*college i.stacked*married if gender==1, robust abs(citizen)
est store empl2male

xi: areg income neweuxd2y* stacked#year if gender==1, robust abs(citizen)
est store income2male0
xi: areg income neweuxd2y* stacked#year i.stacked*age i.stacked*age2 i.stacked*highschool i.stacked*college i.stacked*married if gender==1, robust abs(citizen)
est store income2male

*  Females
xi: areg legaljob neweuxd2y* stacked#year if gender==2, robust abs(citizen)
est store legaljob2female0
xi: areg legaljob neweuxd2y* stacked#year i.stacked*age i.stacked*age2 i.stacked*highschool i.stacked*college i.stacked*married if gender==2, robust abs(citizen)
est store legaljob2female

xi: areg illegaljob neweuxd2y* stacked#year if gender==2, robust abs(citizen)
est store illegaljob2female0
xi: areg illegaljob neweuxd2y* stacked#year i.stacked*age i.stacked*age2 i.stacked*highschool i.stacked*college i.stacked*married if gender==2, robust abs(citizen)
est store illegaljob2female

xi: areg empl neweuxd2y* stacked#year if gender==2, robust abs(citizen)
est store empl2female0
xi: areg empl neweuxd2y* stacked#year i.stacked*age i.stacked*age2 i.stacked*highschool i.stacked*college i.stacked*married if gender==2, robust abs(citizen)
est store empl2female

xi: areg income neweuxd2y* stacked#year if gender==2, robust abs(citizen)
est store income2female0
xi: areg income neweuxd2y* stacked#year i.stacked*age i.stacked*age2 i.stacked*highschool i.stacked*college i.stacked*married if gender==2, robust abs(citizen)
est store income2female


coefplot legaljob2male legaljob2female ||  illegaljob2male illegaljob2female || empl2male empl2female || income2male income2female, byopts(yrescale) vertical keep(neweuxd2y*) xline(1.5) xlabel(1 "[t-2,t-1]" 2 "[t+1,t+2]" 3 "[t+3,t+4]" 4 "[t+5,t+6]") yline(0)



********************************************************************************
*  Appendix Table A2. Panel a. Males
*  The effect of the EU enlargements on labor market outcomes of new EU citizens.

xi: areg legaljob neweuxpost stacked#year if gender==1, robust abs(citizen)
outreg2 using stackeddid, replace $opt
xi: areg legaljob neweuxpost stacked#year i.stacked*age i.stacked*age2 i.stacked*highschool i.stacked*college i.stacked*married if gender==1, robust abs(citizen)
outreg2 using stackeddid, append $opt

xi: areg illegaljob neweuxpost stacked#year if gender==1, robust abs(citizen)
outreg2 using stackeddid, append $opt
xi: areg illegaljob neweuxpost stacked#year i.stacked*age i.stacked*age2 i.stacked*highschool i.stacked*college i.stacked*married if gender==1, robust abs(citizen)
outreg2 using stackeddid, append $opt

xi: areg income neweuxpost stacked#year if gender==1, robust abs(citizen)
outreg2 using stackeddid, append $opt
xi: areg income neweuxpost stacked#year i.stacked*age i.stacked*age2 i.stacked*highschool i.stacked*college i.stacked*married if gender==1, robust abs(citizen)
outreg2 using stackeddid, append $opt


********************************************************************************
*  Appendix Table A2. Panel b. Females
*  The effect of the EU enlargements on labor market outcomes of new EU citizens.

xi: areg legaljob neweuxpost stacked#year if gender==2, robust abs(citizen)
outreg2 using stackeddid, append $opt
xi: areg legaljob neweuxpost stacked#year i.stacked*age i.stacked*age2 i.stacked*highschool i.stacked*college i.stacked*married if gender==2, robust abs(citizen)
outreg2 using stackeddid, append $opt

xi: areg illegaljob neweuxpost stacked#year if gender==2, robust abs(citizen)
outreg2 using stackeddid, append $opt
xi: areg illegaljob neweuxpost stacked#year i.stacked*age i.stacked*age2 i.stacked*highschool i.stacked*college i.stacked*married if gender==2, robust abs(citizen)
outreg2 using stackeddid, append $opt

xi: areg income neweuxpost stacked#year if gender==2, robust abs(citizen)
outreg2 using stackeddid, append $opt
xi: areg income neweuxpost stacked#year i.stacked*age i.stacked*age2 i.stacked*highschool i.stacked*college i.stacked*married if gender==2, robust abs(citizen)
outreg2 using stackeddid, append $opt




clear
set more off
use "$ismu\ismudescriptives.dta" , clear


********************************************************************************
*  Appendix Table A10: Regular and Irregular migrants
*  ISMU survey, summary statistics for regular and irregular immigrants


rename citizenship cb
gen eg=.
replace eg=1 if cb==203 | cb==206 | cb==212 | cb==214 | cb==216 | cb==221 | cb==226 | cb==232 | cb==219 | cb==240 | cb==215 | cb==220 | cb==234 | cb==239 // eu15 
replace eg=2 if cb==257 | cb==315 | cb==247 | cb==248 | cb==249 | cb==227 | cb==233 | cb==255 | cb==251 | cb==244 
replace eg=3 if cb==209 | cb==235 // eu2007
replace eg=4 if cb==224 | cb==250 | cb==201 | cb==252 | cb==223 | cb==272 | cb==253 | cb==270 | cb==271 | cb==351 | cb==202 | cb==256 | cb==925 | cb==225 | cb==959 | cb==254 | cb==229 | cb==231 | cb==245 | cb==236 | cb==246 | cb==243 // euother
replace eg=5 if cb>=401 & cb<=467 // africa
replace eg=6 if (cb>=301 & cb<=314) | (cb>=316 & cb<=350) | (cb>=352 & cb<=364) | cb==703 | cb==708 | cb==712 | cb==713 | cb==715 | cb==720 | cb==721 | cb==725 | cb==727 | cb==730 | cb==731 | cb==732 // asia e
replace eg=7 if cb==503 | cb==505 | cb==506 | cb==507 | cb==513 | cb==514 | cb==515 | cb==516 | cb==517 | cb==518 | cb==519 | cb==523 | cb==524 | cb==525 | cb==527 | cb==529 | cb==530 | cb==532 | cb==533 | cb==534 | cb==602 | cb==604 | cb==605 | cb==606 | cb==608 | cb==609 | cb==612 | cb==614 | cb==615 | cb==616 | cb==617 | cb==618 | cb==619 // america s
replace eg=8 if cb==509 | cb==536 | cb==241 | cb==701 | cb==326 | cb==319 | cb==719  // oecd
lab define egl 1 "EU15" 2 "EU2004" 3 "EU2007" 4 "EU_Other" 5 "Africa" 6 "Asia" 7 "South America" 8 "OECD"
lab values eg egl

tab eg, gen(egg)
rename egg1 EU2004
rename egg2 EU2007
rename egg3 EU_Other
rename egg4 Africa
rename egg5 Asia
rename egg6 South_America
rename egg7 OECD
tab eu2004
tab eu2007

gl cov age married chldrn_tot high employed legaljob illegaljob income EU2004 EU2007 EU_Other Africa Asia South_America OECD 
tabstat $cov if legal==1, by(male ) stat(mean sd N) format(%9.2f)
tabstat $cov if legal==0, by(male ) stat(mean sd N) format(%9.2f)


